<?php
class Passenger extends CS_Controller
{
    public function _init()
    {
        $this->load->helper(array('common'));
        $this->load->model('visitor_model', 'visitor');
    }

    /**
     * 常用游客列表
     */
    public function passengerList()
    {
        $this->data['passengerList'] = $this->visitor->findByUid($this->uid);
        $this->load->view('home/passenger/passengerList', $this->data);
    }

    /**
     * 添加
     */
    public function passengerInfoAdd()
    {
        $this->load->view('home/passenger/passengerInfoAdd', $this->data);
    }

    /**
     * 插入
     */
    public function addPost()
    {
        $postData = $this->input->post();
        $postData['uid'] = $this->uid;
        $error = $this->_validate($postData);
        
        if ($error) {
            $this->alertJumpPre($error);
        }

        $state = $this->visitor->visitorInsert($postData);
        if ($state) {
            $this->success('home/passenger/passengerList', '', '新增成功！');
        } else {
            $this->error('home/passenger/passengerList', '', '新增失败！');
        }
    }

    /**
     * 修改
     */
    public function passengerInfoEdit($visitorId)
    {
        $info = $this->_exists($visitorId);
        if (!$info) {
            $this->alertJumpPre('意外的错误，请刷新后再次尝试。');
        }
        $this->data['info'] = $info;
        $this->load->view('home/passenger/passengerInfoEdit', $this->data);
    }

    /**
     * 更新
     */
    public function editPost()
    {
        $postData = $this->input->post();
        $error = $this->_validate($postData);
        if ($error) {
            $this->alertJumpPre($error);
        }

        if (!$this->_exists($postData['visitor_id'])) {
            $this->alertJumpPre('联系人不存在');
        }

        $state = $this->visitor->visitorUpdate($postData);
        if ($state) {
            $this->success('home/passenger/passengerList', '', '编辑成功！');
        } else {
            $this->error('home/passenger/passengerList', '', '编辑失败！');
        }
    }

    /**
     * 删除
     */
    public function passengerInfoDelete($visitorId)
    {
        if (!$this->_exists($visitorId)) {
            $this->jsonMessage('联系人不存在');
        }
        $state = $this->visitor->visitorDelete($visitorId, $this->uid);
        if ( $state ) {
            $this->success('home/passenger/passengerList', '', '删除成功');
        } else {
            $this->error('home/passenger/passengerList', '', '删除失败');
        }
    }

    /**
     * 设置默认联系人
     */
    public  function setDefault($visitorId)
    {
        if ($visitorId <= 0) {
            $this->error('home/passenger/passengerList', '', '操作失败');
        }
        $info = $this->_exists($visitorId);
        if (!$info) {
            $this->error('home/passenger/passengerList', '', '操作失败');
        }
        $this->db->trans_start();
        $this->visitor->setNoDefault($this->uid);
        $visitor = $this->visitor->setDefaultToId($visitorId);
        $this->db->trans_complete();
        if (!$visitor || $this->db->trans_status() === FALSE) {
            $this->error('home/passenger/passengerList', '', '操作失败');
        } else {
            $this->success('home/passenger/passengerList', '', '操作成功');
        }
    }

    private function _validate($postData)
    {
        if (2 > mb_strlen($postData['visitor_name']) OR !validateChineseName($postData['visitor_name'])) {
            return '真实姓名请与证件姓名一致';
        }
        if (empty($postData['sex']) || $postData['sex'] < 1) {
            return '请选择性别';
        }
        if (!validateMobilePhone($postData['mobile_phone'])) {
            return '请输入正确的手机号码';
        }
        if (empty($postData['type']) || $postData['type'] < 1) {
            return '请选择证件类型';
        }
        if (!checkSfz($postData['pin'])) {
            return '请输入正确的身份证号码';
        }

        return null;
    }

    /**
     * 是否存在
     */
    private function _exists($visitorId)
    {
        $result = $this->visitor->findById($visitorId);
        if ($result->num_rows() == 0) {
            return FALSE;
        }
        return $result->row();
    }
}


